import 'package:get/get.dart';
import 'package:shop/tools/httpsClient.dart';

import '../../../../models/categoryModel.dart';

class CategoryController extends GetxController {
  //TODO: Implement CategoryController
  HttpsClient httpsClient = HttpsClient();
  RxInt selectIndex = 0.obs;
  RxList<CategoryItemModel> leftList = <CategoryItemModel>[].obs;
  RxList<CategoryItemModel> rightList = <CategoryItemModel>[].obs;

  @override
  void onInit() {
    super.onInit();
    //   获取左侧列表数据
    getLeftList();
  }

  void changeSelectIndex(index, id) {
    selectIndex.value = index;
    getRightList(id);
    update();
  }

  //   获取左侧列表的数据
  void getLeftList() async {
    var response = await httpsClient.get('/api/pcate');
    if (response != null) {
      var categoryList = CategoryModel.fromJson(response.data);
      leftList.value = categoryList.result ?? [];
      getRightList(leftList[0].sId!);
      update();
    }
  }

  //   获取右侧列表的数据
  void getRightList(String sid) async {
    var response = await httpsClient.get('/api/pcate?pid=${sid}');
    if (response != null) {
      var categoryList = CategoryModel.fromJson(response.data);
      rightList.value = categoryList.result ?? [];
      update();
    }
  }
}
